﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GraduationProject.RDBMS
{
    public enum NovelSortType
    {
        /// <summary>
        /// 点赞数
        /// </summary>
        AgreeNum = 1,
        /// <summary>
        /// 反对数
        /// </summary>
        DisagreeNum = 2,
        /// <summary>
        /// 点击量
        /// </summary>
        ClickNum = 3,
        /// <summary>
        /// 按照用户发表时间
        /// </summary>
        CreateTime = 4
    }
    public class Novel : OpearteBase
    {
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="Novels"></param>
        /// <returns></returns>
        public bool TryInsertNovel(out bool ExcuteResult, params Model.Novel[] Novels)
        {
            ExcuteResult = false;
            try
            {
                if (Novels != null && Novels.Count() > 0)
                {

                    //打开数据库事务
                    TryOpenTrans();
                    //更新数据库
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("INSERT INTO [dbo].[Novel]");
                    Str_SQL.AppendLine("([SortType]");
                    Str_SQL.AppendLine(",[NovelTitle]");
                    Str_SQL.AppendLine(",[NovelSummary]");
                    Str_SQL.AppendLine(",[CreateTime]");
                    Str_SQL.AppendLine(",[CreateUser]");
                    Str_SQL.AppendLine(",[NovelBody]");
                    Str_SQL.AppendLine(",[IsBan]");
                    Str_SQL.AppendLine(",[IsAdminBan]");
                    Str_SQL.AppendLine(",[AgreeNum]");
                    Str_SQL.AppendLine(",[DisagreeNum]");
                    Str_SQL.AppendLine(",[ClickNum]");
                    Str_SQL.AppendLine(",[IsTop])");
                    Str_SQL.AppendLine("VALUES");
                    Str_SQL.AppendLine("(@SortType");
                    Str_SQL.AppendLine(",@NovelTitle");
                    Str_SQL.AppendLine(",@NovelSummary");
                    Str_SQL.AppendLine(",@CreateTime");
                    Str_SQL.AppendLine(",@CreateUser");
                    Str_SQL.AppendLine(",@NovelBody");
                    Str_SQL.AppendLine(",@IsBan");
                    Str_SQL.AppendLine(",@IsAdminBan");
                    Str_SQL.AppendLine(",@AgreeNum");
                    Str_SQL.AppendLine(",@DisagreeNum");
                    Str_SQL.AppendLine(",@ClickNum");
                    Str_SQL.AppendLine(",@IsTop)");

                    #endregion

                    if (m_Conn.Execute(Str_SQL.ToString(), Novels, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }

            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="NovelIDs"></param>
        /// <returns></returns>
        public bool TryDeleteNovel(out bool ExcuteResult, params int[] NovelIDs)
        {
            ExcuteResult = false;
            try
            {
                if (NovelIDs != null && NovelIDs.Length > 0)
                {
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("DELETE FROM [dbo].[Novel]");
                    Str_SQL.AppendLine("WHERE [dbo].[Novel].[NovelID] IN @IDs");
                    #endregion

                    TryOpenTrans();
                    if (m_Conn.Execute(Str_SQL.ToString(), new { IDs = NovelIDs }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 更新文章
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="Novels"></param>
        /// <returns></returns>
        public bool TryUpdateNovel(out bool ExcuteResult, params Model.Novel[] Novels)
        {
            ExcuteResult = false;
            try
            {
                if (Novels != null && Novels.Length > 0)
                {
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("UPDATE [dbo].[Novel]");
                    Str_SQL.AppendLine("SET [SortType] = @SortType");
                    Str_SQL.AppendLine(",[NovelTitle] = @NovelTitle");
                    Str_SQL.AppendLine(",[NovelSummary] = @NovelSummary");
                    Str_SQL.AppendLine(",[CreateTime] = @CreateTime");
                    Str_SQL.AppendLine(",[CreateUser] = @CreateUser");
                    Str_SQL.AppendLine(",[NovelBody] = @NovelBody");
                    Str_SQL.AppendLine(",[IsBan] = @IsBan");
                    Str_SQL.AppendLine(",[IsAdminBan] = @IsAdminBan");
                    Str_SQL.AppendLine(",[AgreeNum] = @AgreeNum");
                    Str_SQL.AppendLine(",[DisagreeNum] = @DisagreeNum");
                    Str_SQL.AppendLine(",[ClickNum] = @ClickNum");
                    Str_SQL.AppendLine(",[IsTop] = @IsTop");
                    Str_SQL.AppendLine("WHERE [dbo].[Novel].[NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    if (m_Conn.Execute(Str_SQL.ToString(), Novels, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 查询文章
        /// </summary>
        /// <param name="Novels"></param>
        /// <param name="NovelIDs"></param>
        /// <returns></returns>
        public bool TrySelectNovel(out Model.Novel[] Novels, params int[] NovelIDs)
        {
            //预先定义返回值
            Novels = null;
            try
            {
                StringBuilder Str_SQL_Where = new StringBuilder();
                #region 封装Where条件语句
                if (NovelIDs != null && NovelIDs.Length > 0)
                {
                    if (Str_SQL_Where.Length == 0)
                    {
                        Str_SQL_Where.AppendLine("WHERE");
                    }
                    if (Str_SQL_Where.Length > 7)
                    {
                        Str_SQL_Where.AppendLine("AND");
                    }
                    if (Str_SQL_Where.Length >= 7)
                    {
                        Str_SQL_Where.AppendLine("[dbo].[Novel].[NovelID] IN @IDs");
                    }
                }
                #endregion

                StringBuilder Str_Sql = new StringBuilder();
                #region 封装SQL语句
                Str_Sql.AppendLine("SELECT");
                Str_Sql.AppendLine("dbo.Novel.NovelID,");
                Str_Sql.AppendLine("dbo.Novel.SortType,");
                Str_Sql.AppendLine("dbo.Novel.NovelTitle,");
                Str_Sql.AppendLine("dbo.Novel.NovelSummary,");
                Str_Sql.AppendLine("dbo.Novel.CreateTime,");
                Str_Sql.AppendLine("dbo.Novel.CreateUser,");
                Str_Sql.AppendLine("dbo.Novel.NovelBody,");
                Str_Sql.AppendLine("dbo.Novel.IsBan,");
                Str_Sql.AppendLine("dbo.Novel.IsAdminBan,");
                Str_Sql.AppendLine("dbo.Novel.AgreeNum,");
                Str_Sql.AppendLine("dbo.Novel.DisagreeNum,");
                Str_Sql.AppendLine("dbo.Novel.ClickNum,");
                Str_Sql.AppendLine("dbo.Novel.IsTop,");
                Str_Sql.AppendLine("dbo.Novel.IsReview,");
                Str_Sql.AppendLine("dbo.[User].UserName AS CreateUserName,");
                Str_Sql.AppendLine("dbo.SortList.SortName AS SortTypeName");
                Str_Sql.AppendLine("FROM");
                Str_Sql.AppendLine("dbo.Novel");
                Str_Sql.AppendLine("INNER JOIN dbo.[User] ON dbo.Novel.CreateUser = dbo.[User].UserID");
                Str_Sql.AppendLine("INNER JOIN dbo.SortList ON dbo.Novel.SortType = dbo.SortList.SortID");

                //判断是否存在查询条件
                if (Str_SQL_Where.Length > 0)
                {
                    Str_Sql.Append(Str_SQL_Where);
                }
                #endregion

                var SQL_Data = m_Conn.Query<Model.Novel>(Str_Sql.ToString(), new { IDs = NovelIDs }, m_Transaction);
                if (SQL_Data != null && SQL_Data.Count() > 0)
                {
                    Novels = SQL_Data.ToArray();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return false;
        }
        /// <summary>
        /// 查询置顶
        /// </summary>
        /// <param name="Novels"></param>
        /// <param name="NovelIDs"></param>
        /// <returns></returns>
        public bool TrySelectTopNovel(out Model.Novel[] Novels,bool IsTop = true)
        {
            //预先定义返回值
            Novels = null;
            try
            {
                StringBuilder Str_Sql = new StringBuilder();
                #region 封装SQL语句
                Str_Sql.AppendLine("SELECT");
                Str_Sql.AppendLine("dbo.Novel.NovelID,");
                Str_Sql.AppendLine("dbo.Novel.SortType,");
                Str_Sql.AppendLine("dbo.Novel.NovelTitle,");
                Str_Sql.AppendLine("dbo.Novel.NovelSummary,");
                Str_Sql.AppendLine("dbo.Novel.CreateTime,");
                Str_Sql.AppendLine("dbo.Novel.CreateUser,");
                Str_Sql.AppendLine("dbo.Novel.NovelBody,");
                Str_Sql.AppendLine("dbo.Novel.IsBan,");
                Str_Sql.AppendLine("dbo.Novel.IsAdminBan,");
                Str_Sql.AppendLine("dbo.Novel.AgreeNum,");
                Str_Sql.AppendLine("dbo.Novel.DisagreeNum,");
                Str_Sql.AppendLine("dbo.Novel.ClickNum,");
                Str_Sql.AppendLine("dbo.Novel.IsTop,");
                Str_Sql.AppendLine("dbo.Novel.IsReview,");
                Str_Sql.AppendLine("dbo.[User].UserName AS CreateUserName,");
                Str_Sql.AppendLine("dbo.SortList.SortName AS SortTypeName");
                Str_Sql.AppendLine("FROM");
                Str_Sql.AppendLine("dbo.Novel");
                Str_Sql.AppendLine("INNER JOIN dbo.[User] ON dbo.Novel.CreateUser = dbo.[User].UserID");
                Str_Sql.AppendLine("INNER JOIN dbo.SortList ON dbo.Novel.SortType = dbo.SortList.SortID");
                Str_Sql.AppendLine("WHERE");
                Str_Sql.AppendLine("[dbo].[Novel].[IsTop] = @IsTop");
                Str_Sql.AppendLine("Order By dbo.Novel.CreateTime");
                #endregion

                var SQL_Data = m_Conn.Query<Model.Novel>(Str_Sql.ToString(), new { IsTop }, m_Transaction);
                if (SQL_Data != null && SQL_Data.Count() > 0)
                {
                    Novels = SQL_Data.ToArray();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return false;
        }
        /// <summary>
        /// 分页查询文章
        /// </summary>
        /// <returns></returns>
        public bool TrySelectNovelByPage(out Model.Novel[] Novels, out int TotalItemCount, int PageNumber = 1, int PageSize = 20, int AuthorID = 0, NovelSortType SortType = NovelSortType.CreateTime,int NovelSortTypeID = 0,int CreateUserID = 0,bool IsReview = true)
        {
            //预先赋初始值
            Novels = null;
            TotalItemCount = 0;
            try
            {
                StringBuilder Str_SQL_Where = new StringBuilder();
                #region 封装查询条件SQL语句
                if (AuthorID != 0)
                {
                    if (Str_SQL_Where.Length == 0)
                    {
                        Str_SQL_Where.AppendLine("Where");
                    }
                    if (Str_SQL_Where.Length > 7)
                    {
                        Str_SQL_Where.AppendLine("AND");
                    }
                    if (Str_SQL_Where.Length >= 7)
                    {
                        Str_SQL_Where.AppendLine("dbo.Novel.CreateUser = @AuthorID");
                    }
                }
                if (NovelSortTypeID != 0)
                {
                    if (Str_SQL_Where.Length == 0)
                    {
                        Str_SQL_Where.AppendLine("Where");
                    }
                    if (Str_SQL_Where.Length > 7)
                    {
                        Str_SQL_Where.AppendLine("AND");
                    }
                    if (Str_SQL_Where.Length >= 7)
                    {
                        Str_SQL_Where.AppendLine("dbo.Novel.SortType = @NovelSortTypeID");
                    }
                }
                if (CreateUserID != 0)
                {
                    if (Str_SQL_Where.Length == 0)
                    {
                        Str_SQL_Where.AppendLine("Where");
                    }
                    if (Str_SQL_Where.Length > 7)
                    {
                        Str_SQL_Where.AppendLine("AND");
                    }
                    if (Str_SQL_Where.Length >= 7)
                    {
                        Str_SQL_Where.AppendLine("dbo.Novel.CreateUser = @CreateUserID");
                    }
                }
                if (IsReview)
                {
                    if (Str_SQL_Where.Length == 0)
                    {
                        Str_SQL_Where.AppendLine("Where");
                    }
                    if (Str_SQL_Where.Length > 7)
                    {
                        Str_SQL_Where.AppendLine("AND");
                    }
                    if (Str_SQL_Where.Length >= 7)
                    {
                        Str_SQL_Where.AppendLine("dbo.Novel.IsReview = @IsReview");
                    }
                }
                #endregion

                StringBuilder Str_SQL_PageSelect = new StringBuilder();
                #region 封装查询语句
                Str_SQL_PageSelect.AppendLine("SELECT");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.NovelID,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.SortType,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.NovelTitle,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.NovelSummary,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.CreateTime,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.CreateUser,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.NovelBody,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.IsBan,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.IsAdminBan,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.AgreeNum,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.DisagreeNum,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.ClickNum,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.IsTop,");
                Str_SQL_PageSelect.AppendLine("dbo.Novel.IsReview,");
                Str_SQL_PageSelect.AppendLine("dbo.[User].UserName AS CreateUserName,");
                Str_SQL_PageSelect.AppendLine("dbo.SortList.SortName AS SortTypeName");
                Str_SQL_PageSelect.AppendLine("FROM");
                Str_SQL_PageSelect.AppendLine("dbo.Novel");
                Str_SQL_PageSelect.AppendLine("INNER JOIN dbo.[User] ON dbo.Novel.CreateUser = dbo.[User].UserID");
                Str_SQL_PageSelect.AppendLine("INNER JOIN dbo.SortList ON dbo.Novel.SortType = dbo.SortList.SortID");

                //判断是否存在查询条件
                if (Str_SQL_Where.Length > 0)
                {
                    Str_SQL_PageSelect.Append(Str_SQL_Where);
                }

                var Str_SQL_OrderBy = new StringBuilder();
                #region 封装排序/分页语句
                switch (SortType)
                {
                    case RDBMS.NovelSortType.AgreeNum:
                        if (Str_SQL_OrderBy.Length == 0)
                        {
                            Str_SQL_OrderBy.AppendLine("Order By");
                        }
                        if (Str_SQL_OrderBy.Length > 10)
                        {
                            Str_SQL_OrderBy.AppendLine(",");
                        }
                        if (Str_SQL_OrderBy.Length >= 10)
                        {
                            Str_SQL_OrderBy.AppendLine("dbo.Novel.AgreeNum DESC");
                        }
                        break;
                    case RDBMS.NovelSortType.DisagreeNum:
                        if (Str_SQL_OrderBy.Length == 0)
                        {
                            Str_SQL_OrderBy.AppendLine("Order By");
                        }
                        if (Str_SQL_OrderBy.Length > 10)
                        {
                            Str_SQL_OrderBy.AppendLine(",");
                        }
                        if (Str_SQL_OrderBy.Length >= 10)
                        {
                            Str_SQL_OrderBy.Append("dbo.Novel.DisagreeNum DESC");
                        }
                        break;
                    case RDBMS.NovelSortType.ClickNum:
                        if (Str_SQL_OrderBy.Length == 0)
                        {
                            Str_SQL_OrderBy.AppendLine("Order By");
                        }
                        if (Str_SQL_OrderBy.Length > 10)
                        {
                            Str_SQL_OrderBy.AppendLine(",");
                        }
                        if (Str_SQL_OrderBy.Length >= 10)
                        {
                            Str_SQL_OrderBy.Append("dbo.Novel.ClickNum DESC");
                        }
                        break;
                    case RDBMS.NovelSortType.CreateTime:
                        if (Str_SQL_OrderBy.Length == 0)
                        {
                            Str_SQL_OrderBy.AppendLine("Order By");
                        }
                        if (Str_SQL_OrderBy.Length > 10)
                        {
                            Str_SQL_OrderBy.AppendLine(",");
                        }
                        if (Str_SQL_OrderBy.Length >= 10)
                        {
                            Str_SQL_OrderBy.Append("dbo.Novel.CreateTime DESC");
                        }
                        break;
                    default:
                        if (Str_SQL_OrderBy.Length == 0)
                        {
                            Str_SQL_OrderBy.AppendLine("Order By");
                        }
                        if (Str_SQL_OrderBy.Length > 10)
                        {
                            Str_SQL_OrderBy.AppendLine(",");
                        }
                        if (Str_SQL_OrderBy.Length >= 10)
                        {
                            Str_SQL_OrderBy.Append("dbo.Novel.CreateTime DESC");
                        }
                        break;
                }
                Str_SQL_OrderBy.AppendLine(" OFFSET (@PageNumber - 1) * @PageSize ROW FETCH NEXT @PageSize ROW ONLY ");
                #endregion
                //判断是否存在排序/分页条件
                if (Str_SQL_OrderBy.Length > 0)
                {
                    Str_SQL_PageSelect.Append(Str_SQL_OrderBy);
                }
                #endregion

                StringBuilder Str_SQL_SelectCount = new StringBuilder();
                #region 封装数量查询语句
                Str_SQL_SelectCount.AppendLine("SELECT");
                Str_SQL_SelectCount.AppendLine("Count(dbo.Novel.NovelID)");
                Str_SQL_SelectCount.AppendLine("FROM");
                Str_SQL_SelectCount.AppendLine("dbo.Novel");
                Str_SQL_SelectCount.AppendLine("INNER JOIN dbo.[User] ON dbo.Novel.CreateUser = dbo.[User].UserID");

                //判断是否存在查询条件
                if (Str_SQL_Where.Length > 0)
                {
                    Str_SQL_SelectCount.Append(Str_SQL_Where);
                }
                #endregion

                //获取总条数
                TotalItemCount = m_Conn.ExecuteScalar<int>(Str_SQL_SelectCount.ToString(), new
                {
                    AuthorID,
                    PageNumber,
                    PageSize,
                    NovelSortTypeID,
                    CreateUserID,
                    IsReview
                });

                //判断是否存在数据
                if (TotalItemCount == 0)
                {
                    return false;
                }

                //获取分页数据
                var SQL_Data = m_Conn.Query<Model.Novel>(Str_SQL_PageSelect.ToString(), new
                {
                    AuthorID,
                    PageNumber,
                    PageSize,
                    NovelSortTypeID,
                    CreateUserID,
                    IsReview
                });
                //判断是否存在数据
                if (SQL_Data != null && SQL_Data.Count() > 0)
                {
                    Novels = SQL_Data.ToArray();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return false;
        }
        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <param name="NovelID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool TryAddAgreeNum(out bool ExcuteResult, int NovelID, int UserID)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0 && UserID != 0)
                {
                    var Str_SQL_Exist = new StringBuilder();
                    #region 封装查重语句
                    Str_SQL_Exist.AppendLine("SELECT");
                    Str_SQL_Exist.AppendLine("Count(dbo.NovelAgreeList.ID)");
                    Str_SQL_Exist.AppendLine("FROM");
                    Str_SQL_Exist.AppendLine("dbo.NovelAgreeList");
                    Str_SQL_Exist.AppendLine("WHERE");
                    Str_SQL_Exist.AppendLine("dbo.NovelAgreeList.UserID = @UserID AND");
                    Str_SQL_Exist.AppendLine("dbo.NovelAgreeList.NovelID = @NovelID");
                    #endregion
                    if (m_Conn.ExecuteScalar<int>(Str_SQL_Exist.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                    {
                        //存在重复值，不允许点赞
                        ExcuteResult = false;
                        IsDone &= true;
                    }
                    else
                    {
                        //不存在重复值，允许点赞
                        var Str_SQL_Num = new StringBuilder();
                        #region 封装数量更改SQL语句
                        Str_SQL_Num.AppendLine("UPDATE [dbo].[Novel]");
                        Str_SQL_Num.AppendLine("SET [AgreeNum] = [AgreeNum] + 1");
                        Str_SQL_Num.AppendLine("WHERE");
                        Str_SQL_Num.AppendLine("dbo.Novel.NovelID = @NovelID");
                        #endregion

                        //不存在重复值，允许点赞
                        var Str_SQL_Insert = new StringBuilder();
                        #region 封装记录插入SQL语句
                        Str_SQL_Insert.AppendLine("INSERT INTO [dbo].[NovelAgreeList]");
                        Str_SQL_Insert.AppendLine("([UserID]");
                        Str_SQL_Insert.AppendLine(",[NovelID])");
                        Str_SQL_Insert.AppendLine("VALUES");
                        Str_SQL_Insert.AppendLine("(@UserID");
                        Str_SQL_Insert.AppendLine(",@NovelID)");
                        #endregion

                        TryOpenTrans();

                        //执行数量更改语句
                        if (m_Conn.Execute(Str_SQL_Num.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }

                        //执行记录插入语句
                        if (m_Conn.Execute(Str_SQL_Insert.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 文章修改点赞量（管理员功能,默认置零）
        /// </summary>
        /// <returns></returns>
        public bool TryUpdateAgreeNum(out bool ExcuteResult, int NovelID, int AgreeNum = 0)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    var Str_SQL_Update = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Update.AppendLine("UPDATE [dbo].[Novel]");
                    Str_SQL_Update.AppendLine("SET [AgreeNum] = @AgreeNum");
                    Str_SQL_Update.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    var Str_SQL_Delete = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Delete.AppendLine("Delete [dbo].[NovelAgreeList]");
                    Str_SQL_Delete.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    //进行修改操作
                    if (m_Conn.Execute(Str_SQL_Update.ToString(), new { NovelID, AgreeNum }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }

                    //进行记录删除操作
                    if (m_Conn.Execute(Str_SQL_Delete.ToString(), new { NovelID }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 文章点反对
        /// </summary>
        /// <param name="NovelID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool TrySubDisAgreeNum(out bool ExcuteResult, int NovelID, int UserID)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0 && UserID != 0)
                {
                    var Str_SQL_Exist = new StringBuilder();
                    #region 封装查重语句
                    Str_SQL_Exist.AppendLine("SELECT");
                    Str_SQL_Exist.AppendLine("Count(dbo.NovelDisAgreeList.ID)");
                    Str_SQL_Exist.AppendLine("FROM");
                    Str_SQL_Exist.AppendLine("dbo.NovelDisAgreeList");
                    Str_SQL_Exist.AppendLine("WHERE");
                    Str_SQL_Exist.AppendLine("dbo.NovelDisAgreeList.UserID = @UserID AND");
                    Str_SQL_Exist.AppendLine("dbo.NovelDisAgreeList.NovelID = @NovelID");
                    #endregion
                    if (m_Conn.ExecuteScalar<int>(Str_SQL_Exist.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                    {
                        //存在重复值，不允许点反对
                        ExcuteResult = false;
                        IsDone &= true;
                    }
                    else
                    {
                        //不存在重复值，允许点反对
                        var Str_SQL_Num = new StringBuilder();
                        #region 封装数量更改SQL语句
                        Str_SQL_Num.AppendLine("UPDATE [dbo].[Novel]");
                        Str_SQL_Num.AppendLine("SET [DisAgreeNum] = [DisAgreeNum] + 1");
                        Str_SQL_Num.AppendLine("WHERE");
                        Str_SQL_Num.AppendLine("dbo.Novel.NovelID = @NovelID");
                        #endregion

                        //不存在重复值，允许点反对
                        var Str_SQL_Insert = new StringBuilder();
                        #region 封装记录插入SQL语句
                        Str_SQL_Insert.AppendLine("INSERT INTO [dbo].[NovelDisAgreeList]");
                        Str_SQL_Insert.AppendLine("([UserID]");
                        Str_SQL_Insert.AppendLine(",[NovelID])");
                        Str_SQL_Insert.AppendLine("VALUES");
                        Str_SQL_Insert.AppendLine("(@UserID");
                        Str_SQL_Insert.AppendLine(",@NovelID)");
                        #endregion

                        TryOpenTrans();

                        //执行数量更改语句
                        if (m_Conn.Execute(Str_SQL_Num.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }

                        //执行记录插入语句
                        if (m_Conn.Execute(Str_SQL_Insert.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 文章修改反对量（管理员功能,默认置零）
        /// </summary>
        /// <returns></returns>
        public bool TryUpdateDisAgreeNum(out bool ExcuteResult, int NovelID, int DisAgreeNum = 0)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    var Str_SQL_Update = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Update.AppendLine("UPDATE [dbo].[Novel]");
                    Str_SQL_Update.AppendLine("SET [DisAgreeNum] = @DisAgreeNum");
                    Str_SQL_Update.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    var Str_SQL_Delete = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Delete.AppendLine("Delete [dbo].[NovelDisAgreeList]");
                    Str_SQL_Delete.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    //进行修改操作
                    if (m_Conn.Execute(Str_SQL_Update.ToString(), new { NovelID, DisAgreeNum }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }

                    //进行记录删除操作
                    if (m_Conn.Execute(Str_SQL_Delete.ToString(), new { NovelID }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 文章增加点击量
        /// </summary>
        /// <returns></returns>
        public bool TryAddClickNum(out bool ExcuteResult, int NovelID, int UserID)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0 && UserID != 0)
                {
                    var Str_SQL_Exist = new StringBuilder();
                    #region 封装查重语句
                    Str_SQL_Exist.AppendLine("SELECT");
                    Str_SQL_Exist.AppendLine("Count(dbo.NovelClickList.ID)");
                    Str_SQL_Exist.AppendLine("FROM");
                    Str_SQL_Exist.AppendLine("dbo.NovelClickList");
                    Str_SQL_Exist.AppendLine("WHERE");
                    Str_SQL_Exist.AppendLine("dbo.NovelClickList.UserID = @UserID AND");
                    Str_SQL_Exist.AppendLine("dbo.NovelClickList.NovelID = @NovelID");
                    #endregion
                    if (m_Conn.ExecuteScalar<int>(Str_SQL_Exist.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                    {
                        //存在重复值，不允许点反对
                        ExcuteResult = false;
                        IsDone &= true;
                    }
                    else
                    {
                        //不存在重复值，允许点反对
                        var Str_SQL_Num = new StringBuilder();
                        #region 封装数量更改SQL语句
                        Str_SQL_Num.AppendLine("UPDATE [dbo].[Novel]");
                        Str_SQL_Num.AppendLine("SET [ClickNum] = [ClickNum] + 1");
                        Str_SQL_Num.AppendLine("WHERE");
                        Str_SQL_Num.AppendLine("dbo.Novel.NovelID = @NovelID");
                        #endregion

                        //不存在重复值，允许点反对
                        var Str_SQL_Insert = new StringBuilder();
                        #region 封装记录插入SQL语句
                        Str_SQL_Insert.AppendLine("INSERT INTO [dbo].[NovelClickList]");
                        Str_SQL_Insert.AppendLine("([UserID]");
                        Str_SQL_Insert.AppendLine(",[NovelID])");
                        Str_SQL_Insert.AppendLine("VALUES");
                        Str_SQL_Insert.AppendLine("(@UserID");
                        Str_SQL_Insert.AppendLine(",@NovelID)");
                        #endregion

                        TryOpenTrans();

                        //执行数量更改语句
                        if (m_Conn.Execute(Str_SQL_Num.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }

                        //执行记录插入语句
                        if (m_Conn.Execute(Str_SQL_Insert.ToString(), new { NovelID, UserID }, m_Transaction) > 0)
                        {
                            ExcuteResult = true;
                            IsDone &= true;
                        }
                        else
                        {
                            ExcuteResult = false;
                            IsDone &= false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 文章修改点击量（管理员功能,默认置零）
        /// </summary>
        /// <returns></returns>
        public bool TryUpdateClickNum(out bool ExcuteResult, int NovelID, int ClickNum = 0)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    var Str_SQL_Update = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Update.AppendLine("UPDATE [dbo].[Novel]");
                    Str_SQL_Update.AppendLine("SET [ClickNum] = @ClickNum");
                    Str_SQL_Update.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    var Str_SQL_Delete = new StringBuilder();
                    #region 封装数量修改SQL语句
                    Str_SQL_Delete.AppendLine("Delete [dbo].[NovelClickList]");
                    Str_SQL_Delete.AppendLine("WHERE [NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    //进行修改操作
                    if (m_Conn.Execute(Str_SQL_Update.ToString(), new { NovelID, ClickNum }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }

                    //进行记录删除操作
                    if (m_Conn.Execute(Str_SQL_Delete.ToString(), new { NovelID }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 设置文章置顶
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="NovelID"></param>
        /// <param name="IsTop"></param>
        /// <returns></returns>
        public bool TryNovelTop(out bool ExcuteResult, int NovelID, bool IsTop = false)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("UPDATE [dbo].[Novel]");
                    Str_SQL.AppendLine("SET [dbo].[Novel].[IsTop] = @IsTop");
                    Str_SQL.AppendLine("WHERE [dbo].[Novel].[NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    if (m_Conn.Execute(Str_SQL.ToString(), new { NovelID, IsTop }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 设置文章禁止评论
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="NovelID"></param>
        /// <param name="IsTop"></param>
        /// <returns></returns>
        public bool TryNovelBan(out bool ExcuteResult, int NovelID, bool IsAdmin = false, bool IsBan = false)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("UPDATE [dbo].[Novel]");
                    switch (IsAdmin)
                    {
                        case true:
                            Str_SQL.AppendLine("SET [dbo].[Novel].[IsAdminBan] = @IsBan");
                            break;
                        default:
                            Str_SQL.AppendLine("SET [dbo].[Novel].[IsBan] = @IsBan");
                            break;
                    }
                    Str_SQL.AppendLine("WHERE [dbo].[Novel].[NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    if (m_Conn.Execute(Str_SQL.ToString(), new { NovelID, IsBan }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
        /// <summary>
        /// 设置文章通过审核
        /// </summary>
        /// <param name="ExcuteResult"></param>
        /// <param name="NovelID"></param>
        /// <param name="IsTop"></param>
        /// <returns></returns>
        public bool TryNovelIsReview(out bool ExcuteResult, int NovelID, bool IsAdmin = false, bool IsReview = false)
        {
            ExcuteResult = false;
            try
            {
                if (NovelID != 0)
                {
                    StringBuilder Str_SQL = new StringBuilder();
                    #region 封装SQL语句
                    Str_SQL.AppendLine("UPDATE [dbo].[Novel]");
                    switch (IsAdmin)
                    {
                        case true:
                            Str_SQL.AppendLine("SET [dbo].[Novel].[IsReview] = @IsReview");
                            break;
                        default:
                            Str_SQL.AppendLine("SET [dbo].[Novel].[IsReview] = @IsReview");
                            break;
                    }
                    Str_SQL.AppendLine("WHERE [dbo].[Novel].[NovelID] = @NovelID");
                    #endregion

                    TryOpenTrans();
                    if (m_Conn.Execute(Str_SQL.ToString(), new { NovelID, IsReview }, m_Transaction) > 0)
                    {
                        ExcuteResult = true;
                        IsDone &= true;
                    }
                    else
                    {
                        ExcuteResult = false;
                        IsDone &= false;
                    }
                }
            }
            catch (Exception ex)
            {
                IsDone &= false;
                Console.WriteLine(ex);
            }
            return IsDone;
        }
    }
}